package com.itheima.Test2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
//        	用多线程的方式读取多个文件，要求：一个线程读取一个文件
        ArrayList<String> list = method1();
        for (String s : list) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        readfile(s);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }


    private static void readfile(String s) throws IOException {
        FileReader fr=new FileReader(s);
        //System.out.println("读取成功");
        int len;
        char[] chars=new char[1024];
        while ((len=fr.read(chars))!=-1){
            System.out.println(Thread.currentThread().getName()+new String(chars,0,len));
        }
        fr.close();

    }

    private static ArrayList<String> method1() {
        ArrayList<String> list = new ArrayList<>();
        list.add("D:\\新建文件夹\\1.txt");
        list.add("D:\\新建文件夹\\2.txt");
        list.add("D:\\新建文件夹\\3.txt");
        list.add("D:\\新建文件夹\\4.txt");
        return list;
    }
}
